<template>
	<view class="container user-addrlist">
		<view class="addr-list" v-for="item in pageList" :key="item.id" @click="clickItem(item)">
			<view class="addr-info">
				<view>
					<view class="addr-user">
						<text class="iconfont" style="font-size: 34rpx;margin-right:20rpx;">&#xe639;</text>
						<text style="font-size: 34rpx;color: #232323;margin-right:20rpx;">{{item.name}}</text>
						<text style="font-size: 30rpx;color: #8F8F8F;">{{item.phone}}</text>
					</view>
					<view class="addr-detail ">
						{{item.address}}
					</view>
				</view>
				<text class="iconfont">&#xe775;</text>
			</view>
			<view class="addr-active">
				<view class="set-default" v-if="item.defaultFlag!=='2'" @click.stop="setDefault(item)">
					<text class="iconfont">&#xe63c;</text>
					<text>设为默认</text>
				</view>
				<view class="set-default c-primary" v-else @click.stop="setDefault(item)">
					<text class="iconfont c-primary">&#xe63c;</text>
					<text>默认地址</text>
				</view>
				
				<view class="set-edit c-success" @click.stop="jumpEdit(item.id)">编辑</view>
				<view class="set-edit c-danger" @click.stop="delAddr(item.id)">删除</view>
			</view>
		</view>
		<view v-if="pageList.length===0">
			<view class="null-page">
				<text class="iconfont">&#xe642;</text>
				<view class="null-page-tips">暂无数据,请新增地址~</view>
			</view>
		</view>
		<view class="addr-bottom">
			<view class="yuan-btn btn-success" style="margin-right: 30rpx;" @click="jumpEdit()">新增收货地址</view>
			<!-- <view class="yuan-btn btn-success">微信地址</view> -->
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, ref } from 'vue';
	import { hideLoading, showLoading, showToast } from '@/common/util';
	import api from '@/common/api';
	import { onShow } from '@dcloudio/uni-app'
	import commonStore from '@/store/commonStore';
	import { onLoad } from "@dcloudio/uni-app"
	const isLoading = ref(false)
	const pageList = ref<any[]>([])
	const notMore = ref(true)
	onShow(async () => {
		if (commonStore.state.reload.indexOf('addrList') >= 0) {
			getAddrList()
			commonStore.commit('delReload', 'addrList')
		}
	})
	let pageType = ''
	onLoad((options) => {
		pageType = options.type;
	})
	onMounted(async () => {
		getAddrList()
	})


	const getAddrList = async () => {
		showLoading()
		isLoading.value = true
		try {
			const res = await api.getAddrList()
			pageList.value = res.data || []
		} finally {
			hideLoading()
			isLoading.value = false
		}

	}
	const jumpEdit = (id ?: string) => {
		const editId = id || ''
		uni.navigateTo({ url: '/pages/user/addrEdit?id=' + editId })
	}
	const setDefault = async (item) => {
		if (item.defaultFlag === '2') {
			// 当前为默认地址

		} else {
			showLoading()
			try {
				const res = await api.setAddrDefault({ id: item.id })
				showToast('设置成功')
				getAddrList()
			} finally {
				hideLoading()
			}
		}

	}
	const delAddr = (id : string) => {
		uni.showModal({
			title: '提示',
			content: '你确定要删除该地址吗？',
			success: async (res) => {
				if (res.confirm) {
					showLoading()
					try {
						const res = await api.delAddr({ ids: [id] })
						showToast('删除成功')
						getAddrList()
					} finally {
						hideLoading()
					}
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	const clickItem = (item) => {
		// 如果是选择地址，设置选中并回跳
		if (pageType === 'selectAddr') {
			commonStore.commit('setSelAddr', item)
			uni.navigateBack()
		}

	}
</script>

<style lang="scss">
	.user-addrlist {
		padding: 30rpx 30rpx 130rpx 30rpx;

		.addr-list {
			background-color: #fff;
			box-shadow: 0 8rpx 10rpx 0 rgba(0, 0, 0, 0.1);
			border-radius: 12rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;
		}

		.addr-user {
			display: flex;
			align-items: center;
		}

		.addr-info {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: solid 1px #eee;
			padding-bottom: 15rpx;

			.iconfont {
				flex-shrink: 0;
			}
		}

		.addr-detail {
			font-size: 30rpx;
			color: #8F8F8F;
			margin-top: 15rpx;
		}

		.addr-active {
			margin-top: 15rpx;
			display: flex;
			justify-content: space-between;
			padding: 10rpx 0;
			
		}

		.set-default {
			font-size: 26rpx;
			color: #333333;
			display: flex;
			align-items: center;
			flex-grow: 1;

			.iconfont {
				color: #8F8F8F;
				margin-right: 10rpx;
				display: inline-block;
				font-size: 32rpx;
			}
		}

		.set-edit {
			font-size: 28rpx;
			margin-left: 15rpx;
		}

		.addr-bottom {
			display: flex;
			align-items: center;
			justify-content: center;
			position: fixed;
			height: 120rpx;
			left: 0;
			bottom: 0;
			width: 100%;
			box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.1);
			background-color: #fff;

			.yuan-btn {
				display: inline-flex;
				height: 80rpx;
				border-radius: 70rpx;
				padding: 0 50rpx;
				font-size: 32rpx;
			}
		}
	}
</style>